$(document).ready(function() {
    $('.nav-pg').navDropdown();
    $('.article-side').articleSide();
    $('.box-slider').modifySlider();
    $('#eventmenu').eventNav();
    $('.event-program').divideColumns();
    // Smooth scroll
    /*$('a[href^=#]').click(function(e) {
        e.preventDefault();
        
        var id = $(this).attr('href');
        
        var toScroll = $(id).offset().top;
        var scrollDistance = toScroll - 100;
        
        $('html,body').stop().animate(
            {
                scrollTop: scrollDistance
            },
            {
                duration: 800
            }
        );
    });*/
    //event menu fixed//
    /*if (!$('#eventmenu').length == 0) {
        var eventmenuTop = $('#eventmenu').offset().top - (($(window).width() > 1180) ? $('.wrapper-header').height() : 0);
        var eventmenuHeight = $('#eventmenu').outerHeight();
        $(window).scroll(function() {
           if($(window).scrollTop() >= eventmenuTop) {

                $('#eventmenu')
                    .addClass('scrolled')
                if ($('.placeholder').length == 0) {
                    $('#eventmenu')
                        .before($('<div>')
                            .addClass('placeholder')
                            .css ({
                                'width' : '100%', 
                                'height' : (eventmenuHeight + 'px')
                            }) );
                    }
            } else {

                $('#eventmenu')
                    .removeClass('scrolled');
                $('.placeholder').remove();
            }
        });
    };*/
    //show/hide comment button//
    $('.article-comment textarea').keyup(function() {
        var hasContent = $.trim(this.value).length > 0;
        var isVisible = $('button', this.parentNode).is(":visible");

        if(hasContent && !isVisible) { 
            $('button', this.parentNode)
               .slideDown();
        } else if(!hasContent && isVisible) { 
            $('button', this.parentNode)
                .slideUp();
        }
    });
});

//
WebFontConfig = {
	typekit:{id:'pko8qrb'},
	timeout:2000
};
(function(){
	var wf = document.createElement('script');
	wf.src = ('https:' == document.location.protocol ? 'https' : 'http') + '://ajax.googleapis.com/ajax/libs/webfont/1.4.7/webfont.js';
	wf.type = 'text/javascript';
	wf.async = 'true';
	var s = document.getElementsByTagName('script')[0];
	s.parentNode.insertBefore(wf, s);
})();
//
utils = {
    _stateMediaQuery:function(){
        if(!$('.media-indicator').length) $('body').append('<div class="media-indicator"></div>');
        if(!window.getComputedStyle){
            return 'desktop_IE8'
        }else{
            return window.getComputedStyle($('.media-indicator')[0], ':before').getPropertyValue('content').replace(/\"/g,'');
        }
    }
}
var ArticleSide = function(elem, options){
    this.elem = elem;
    this.$elem = $(elem);
    this.options = options;
    this.loading = false;
};
ArticleSide.prototype = {
    defaults: {
        widthMobile:'100%',
        widthTablet:360,
        widthDesktop:30.61224489795918,
        widthBorder:8
    },
    init:function(){
        var self = this, $btn = $('<a href="#" class="article-side-button">Meer nieuws</a>'), $list = $('.section-content', self.$elem);
        self.config = $.extend({}, self.defaults, this.options);
        $('body').append('<div id="overlay-article-pg"></div>');
        self.$elem.addClass('fixed-side').prepend($btn);
        self._setFixed($btn, $list);
        $(window).on('load resize', function(){
            self._setFixed($btn, $list);
        });
        self._openSide($btn);
        $list.on('load.angiLoadList scroll.angiLoadList', function(){
            self._loadList($list);
        });
        return this;
    },
    _setFixed:function($btn, $list){
        var self = this,
            positionRight,
            widthSide,
            $parent = self.$elem.parent();
        if(utils._stateMediaQuery() == 'desktop' || utils._stateMediaQuery() == 'desktop_IE8'){
            widthSide = ($parent.width() * self.config.widthDesktop) / 100;
            positionRight = $(window).width() - ($parent.offset().left + $parent.width() - widthSide);
            if(self.$elem.hasClass('open')){
                self.$elem.removeClass('open');
                $('#overlay-article-pg').hide();
            }
        }else{
            if(utils._stateMediaQuery() == 'mobile'){
                widthSide = self.config.widthMobile;
            }else{
                widthSide = self.config.widthTablet;
            }
            positionRight = self.config.widthBorder;
            if(self.$elem.hasClass('open')){
                positionRight = (self.$elem.width() + self.config.widthBorder);
            }
        }
        self.$elem.css({
            'width':widthSide,
            'margin-left':-positionRight
        });
        $list.css({
            'height':self.$elem.height() - $list.position().top
        });
        $btn.css({
            top:$(window).height() / 2 + $btn.height() / 2
        })
    },
    _loadList:function($target){
        var self = this, $more = $('.more', $target).last();
        if(!self.loading) {
            if($target.scrollTop() + $target.height() >= $target.prop('scrollHeight')) {
                self.loading = true;
                if($more.length){
                    $.ajax({
                        url: $('a', $more).attr('href'),
                        beforeSend: function () {
                            $more.addClass('loading');
                        }
                    }).done(function (data) {
                        $more.remove();
                        $target.append(data);
                        self.loading = false;
                    }).fail(function () {
                        alert('error');
                    })
                }else{
                    $target.off('scroll.angiLoadList');
                }
            }
        }

    },
    _openSide:function($btn){
        var self = this;
        $($btn).on('click', function(e){
            e.preventDefault();
            self.$elem.stop().animate(
                {'margin-left':self.$elem.hasClass('open') ? -self.config.widthBorder : -(self.$elem.width() + self.config.widthBorder)},
                {duration:800,
                    complete:function(){
                        if(self.$elem.hasClass('open')){
                            self.$elem.removeClass('open');
                        }else{
                            self.$elem.addClass('open');
                        }
                    }
                }
            );
            if(self.$elem.hasClass('open')){
                $('#overlay-article-pg').fadeOut();
            }else{
                $('#overlay-article-pg').fadeIn();
            }
        });
    }
};
ArticleSide.defaults = ArticleSide.prototype.defaults;
$.fn.articleSide = function(options){
    return this.each(function(){
        new ArticleSide(this, options).init();
    });
};
//NAVDROPDOWN//
var NavDropdown = function(elem, options){
    this.elem = elem;
    this.$elem = $(elem);
    this.iterator = 0;
    //this.options = options;
};
NavDropdown.prototype = {
    //defaults: {},
    init:function(){
        var self = this;
        //self.config = $.extend({}, self.defaults, this.options);
        $('body').append('<div id="overlay-nav-pg"></div>');
        self.$elem.before('<a href="#" class="button-nav-pg" title="Menu"><span>Menu</span></a>');
        self._openSmall('.search a.button-search', true);
        self._openSmall('.extra a.button-more');
        self._openSmall('.members a.button-members');
        self._openSmall('.icons a.button-social');
        self._openMobile();
        return this;
    },
    _openSmall:function(current, overlay){
        var self = this, $link = $(current, self.$elem), iterator = Math.floor(Math.random() * 501);
        if($($link.attr('href')).length){
            var $block = $($link.attr('href'));
            $link.on('click', function(e){
                if($block.is(':visible')){
                    $block.stop().slideUp(function(){
                        $link.removeClass('open');
                        $block.removeClass('show').removeAttr('style');
                    });
                    if(overlay) $('#overlay-nav-pg').fadeOut();
                    $(document).off('click.angiNavDropdown' + iterator);
                }else{
                    $link.addClass('open');
                    $block.stop().slideDown(function(){
                        $block.addClass('show').removeAttr('style');
                        $(document).on('click.angiNavDropdown' + iterator, function(ev){
                            if(!$(ev.target).parents().addBack().is($block)) $link.click();
                        });
                    });
                    if(overlay) $('#overlay-nav-pg').fadeIn();
                }
                e.preventDefault();
            });
        }
    },
    _openMobile:function(){
        var self = this, $btn = $('.button-nav-pg');
        $btn.on('click', function(e){
            if(self.$elem.is(':visible')){
                self.$elem.stop().slideUp(function(){
                    $btn.removeClass('open');
                    self.$elem.removeClass('show').removeAttr('style');
                });
            }else{
                $btn.addClass('open');
                self.$elem.stop().slideDown(function(){
                    self.$elem.addClass('show').removeAttr('style');
                });
            }
            e.preventDefault();
        });
    }
};
NavDropdown.defaults = NavDropdown.prototype.defaults;
$.fn.navDropdown = function(options){
    return this.each(function(){
        new NavDropdown(this, options).init();
    });
};
//
var EventNav = function(elem, options){
    this.elem = elem;
    this.$elem = $(elem);
    this.$parent = $(elem).parents();
    //this.options = options;
};
EventNav.prototype = {
    //defaults: {},
    init:function(){
        var self = this,
            $link = $('a', self.$elem),
            windowTop,
            positionTop,
            placeholderBlock = $('<div class="cf"></div>'),
            headerHeight = ((utils._stateMediaQuery() == 'desktop') ? 73 : 0) + self.$elem.outerHeight();
        //self.config = $.extend({}, self.defaults, this.options);
        self.$elem.before(placeholderBlock.css({'height':0}));
        $link.on('click', function(e){
            if($($(this).attr('href')).length){
                e.preventDefault();
                $('html, body').animate({
                    scrollTop:$($(this).attr('href')).offset().top - headerHeight
                });
            }
        });
        $(window).on('load scroll resize', function(){
            headerHeight = ((utils._stateMediaQuery() == 'desktop' || utils._stateMediaQuery() == 'desktop_IE8') ? 73 : 0) + self.$elem.outerHeight();
            windowTop = $(window).scrollTop();
            positionTop = placeholderBlock.offset().top - headerHeight;
            if(windowTop >= positionTop){
                self.$elem.addClass('scrolled');
                placeholderBlock.css({'height':self.$elem.outerHeight()});
            }else{
                self.$elem.removeClass('scrolled');
                placeholderBlock.css({'height':0});
            }
            $link.each(function(){
                var $current = $(this), $currentRef = $($current.attr('href'));
                if(($currentRef.offset().top - headerHeight - 12) <= windowTop && ($currentRef.offset().top - headerHeight) + $currentRef.height() > windowTop){
                    $link.removeClass('active');
                    $current.addClass('active');
                }else{
                    $current.removeClass('active');
                }
            });
        });
        return this;
    }
};
EventNav.defaults = EventNav.prototype.defaults;
$.fn.eventNav = function(options){
    return this.each(function(){
        new EventNav(this, options).init();
    });
};
//
var ModifySlider = function(elem, options){
    this.elem = elem;
    this.$elem = $(elem);
    //this.options = options;
};
ModifySlider.prototype = {
    //defaults: {},
    init:function(){
        var self = this, opt = {};
        //self.config = $.extend({}, self.defaults, this.options);
        if($().flexslider){
            if(self.$elem.parent().hasClass('slider-sponsors')){
                opt = {
                    itemWidth:120,
                    directionNav:true,
                    controlNav:false,
                    randomize:true
                };
            }else if(self.$elem.parent().hasClass('slider-partners')){
                opt = {
                    maxItems:5,
                    minItems:1,
                    itemWidth:200,
                    controlNav:true,
                    directionNav:false
                };
            }else if(self.$elem.parent().hasClass('slider-news')){
                opt = {
                    maxItems:4,
                    minItems:1,
                    itemWidth:300,
                    directionNav:true,
                    controlNav:false,
                    start:function(){
                        self.$elem.eqRow({columnElement:'.slide'});
                    }
                };
            }else if(self.$elem.parent().hasClass('slider-feedback')){
                opt = {
                    maxItems:5,
                    minItems:1,
                    itemWidth:250,
                    directionNav:true,
                    controlNav:true,
                    start:function(){
                        self.$elem.eqRow({columnElement:'.slide'});
                    }
                };
            }
            $(window).load(function(){
                self.$elem.flexslider(opt);
            });
        }
        return this;
    }
};
ModifySlider.defaults = ModifySlider.prototype.defaults;
$.fn.modifySlider = function(options){
    return this.each(function(){
        new ModifySlider(this, options).init();
    });
};
//
var EqRow = function(elem, options){
    this.elem = elem;
    this.$elem = $(elem);
    this.options = options;
};
EqRow.prototype = {
    defaults: {
        columnElement:'article'
    },
    init:function(){
        var self = this;
        self.config = $.extend({}, self.defaults, this.options);
        self._eqHeight();
        $(window).on('load resize', function(){self._eqHeight()});
        return this;
    },
    _eqHeight:function(){
        var self = this, $col = $(self.config.columnElement, self.$elem), $top = 0, current_row_start = 0, current_tallest = 0, row_divs = new Array();
        $col.each(function(){
            var $i_i = $(this);
            $i_i.css({'height':'auto'});
            $top = $i_i.position().top;

            if(current_row_start != $top){
                for(current_div = 0; current_div < row_divs.length; current_div++){
                    row_divs[current_div].height(current_tallest);
                }
                row_divs.length = 0; // empty the array
                current_row_start = $top;
                current_tallest = $i_i.height();
                row_divs.push($i_i);
            }else{
                row_divs.push($i_i);
                current_tallest = (current_tallest < $i_i.height()) ? ($i_i.height()) : (current_tallest);
            }
            for(current_div = 0; current_div < row_divs.length; current_div++){
                row_divs[current_div].height(current_tallest);
            }
        });
    }
};
EqRow.defaults = EqRow.prototype.defaults;
$.fn.eqRow = function(options){
    return this.each(function(){
        new EqRow(this, options).init();
    });
};
//

$.fn.divideColumns = function(params){
  var conf = $.extend({
   colNumber:2,
   itemTag:'dl',
   direction:'vertical'
  }, params);
  this.each(function(){
   var o=$(this), c=conf, total=o.children(c.itemTag).size(), base=Math.ceil(total/c.colNumber);
   for (i=1;i<=c.colNumber;i++){
    o.find(c.itemTag+':first').before($('<div>').addClass('col-'+i).addClass('b-col'));
   }
   if(c.direction=='vertical'){
    var k=1;
    o.children(c.itemTag).each(function(i){
     if((i+1)> base*(c.colNumber-1)){
      o.find('.col-'+c.colNumber).append(this);
     }else{
      if((i+1)<=(base*k)){
       o.find('.col-'+k).append(this);
      }else{
       o.find('.col-'+(k+1)).append(this);
       k=k+1;
      }
     }
    });
   }else{
    var l=0;
    o.children(c.itemTag).each(function(){
     l=l+1;
     if(l<=c.colNumber){
      o.find('.col-'+l).append(this);
     }else{
      l=1;
      o.find('.col-'+l).append(this);
     }
    });
   }
   o.find('.b-col:last').addClass('b-last');
  });
 };
